home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / comm / misc / avmnfaxsrc1_33.lha / faxrender.c < prev    next >
C/C++ Source or Header  |  1994-05-26  |  5KB  |  242 lines

  1. /* $Id: render.c,v 1.2 1993/06/11 16:29:21 Rhialto Exp $
  2.  * $Log: render.c,v $
  3.  * Revision 1.2  1993/06/11  16:29:21  Rhialto
  4.  * First real RCS checkin
  5.  *
  6.  */
  7.  
  8. /*
  9.  *  RENDER.C
  10.  *
  11.  *  David Berezowski - March/88.
  12.  *  Modified for DICE - May/91    Matthew Dillon
  13.  *  Modified for ElCheapoFax April 1993 Olaf 'Rhialto' Seibert.
  14.  *
  15.  *  Copyright (c) 1988  Commodore-Amiga, Inc.
  16.  *  (c)Copyright 1991 Matthew Dillon
  17.  *  (c)Copyright 1993 Olaf Seibert
  18.  */
  19.  
  20. #include <string.h>
  21. #include <devices/printer.h>
  22. #include <devices/prtbase.h>
  23. #include <intuition/preferences.h>
  24.  
  25. #include "faxdefs.h"
  26.  
  27. #include <stdio.h>
  28.  
  29. extern struct PrinterData* PD;
  30. extern struct PrinterExtendedData* PED;
  31.  
  32. extern void Transfer(PrtInfo *, UWORD, UBYTE *);
  33. extern void OpenFaxFile(int);
  34. extern void CloseFaxFile(void);
  35. extern int endPageOnClose(void);
  36. extern void setStatus(char*);
  37. extern void setFileName(char*);
  38.  
  39. void finalizeDoChar(void);
  40.  
  41. char* pd_PrintBuf = 0;
  42.  
  43. int NoFormFeed;
  44. int ped_NumRows = 16;
  45. int currentPage = 1;
  46. int FaxFine = 1;
  47.  
  48. void *FaxHandle = 0;
  49.  
  50. long Render(long ct, long x, long y, long status) {
  51.     static long LineBufSize, TotalBufSize;
  52.     static int XSize;
  53.  
  54.     long err;
  55.  
  56.     err = PDERR_NOERR;
  57.  
  58.     switch(status) {
  59.     case 5: /* Pre-Master Initialization */
  60.     /*
  61.      *    ct    - 0 or pointer to IODRPReq structure.
  62.      *    x    - io_Special flag from IODRPReq.
  63.      *    y    - 0.
  64.      */
  65.  
  66.     /* select density */
  67.       /* SetDensity(x & SPECIAL_DENSITYMASK);*/
  68. //      printf("Pre-Master Init\n");
  69.     break;
  70.     case 0:    /* Master Initialization */
  71.     /*
  72.      *    ct    - pointer to IODRPReq structure.
  73.      *    x    - width of printed picture in pixels.
  74.      *    y    - height of printed picture in pixels.
  75.      */
  76.     XSize = x;
  77.     if (XSize != LINE_BITS) {
  78.         XSize = LINE_BITS;
  79.     }
  80.     NoFormFeed = (((struct IODRPReq *)ct)->io_Special & SPECIAL_NOFORMFEED)
  81.              != 0;
  82.     pd_PrintBuf = NULL;
  83.  
  84.     LineBufSize = (XSize + 7) / 8;
  85.     TotalBufSize = PED->ped_NumRows * LineBufSize;
  86.     pd_PrintBuf = AllocMem(TotalBufSize, MEMF_PUBLIC);
  87.     if (pd_PrintBuf == NULL) {
  88.       err = PDERR_BUFFERMEMORY;
  89.       break;
  90.     }
  91.     break;
  92.     case 1: /* Scale, Dither and Render */
  93.         /*
  94.          *        ct        - pointer to PrtInfo structure.
  95.          *        x        - 0.
  96.          *        y        - row # (0 to Height - 1).
  97.          */
  98. #define pi ((struct PrtInfo *)ct)
  99.     if (pi->pi_xpos >= XSize) {
  100.         pi->pi_xpos = 0;
  101.     }
  102.     Transfer((struct PrtInfo *)ct, y, pd_PrintBuf + LineBufSize * (y % PED->ped_NumRows));
  103.     break;
  104.     case 2: /* Dump Buffer to Printer */
  105.     /*
  106.      *    ct    - 0.
  107.      *    x    - 0.
  108.      *    y    - # of rows sent (1 to peMd_NumRows).
  109.      */
  110.     {
  111.         int         offset;
  112.  
  113.         if (!FaxHandle) {
  114.           /* only open fax file when someone tries to write to it */
  115.           OpenFaxFile(0);
  116.         }
  117.  
  118.         if (FaxHandle) {
  119.           for (offset = 0; y > 0; offset += LineBufSize, y--) {
  120.         tofax(FaxHandle, pd_PrintBuf + offset, XSize);
  121.           }
  122.         }
  123.     }
  124.     break;
  125.     case 3:    /* Clear and Init Buffer */
  126.     /*
  127.      *    ct    - 0.
  128.      *    x    - 0.
  129.      *    y    - 0.
  130.      */
  131.     memset(pd_PrintBuf, 0, TotalBufSize);
  132.     break;
  133.     case 4: /* Close Down */
  134.     /*
  135.      *    ct    - error code.
  136.      *    x    - io_Special flag from IODRPReq.
  137.      *    y    - 0.
  138.      */
  139.  
  140.     if (pd_PrintBuf != NULL) {
  141.         FreeMem(pd_PrintBuf, TotalBufSize);
  142.         pd_PrintBuf = NULL;
  143.     }
  144.         if (endPageOnClose()) {
  145.       CloseFaxFile();
  146.     } else {
  147.       setStatus("Render Closed"); 
  148.     }
  149.     break;
  150.     }
  151.     return(err);
  152. }
  153.  
  154. char lastFaxFile[256];
  155. #define ESC        "\33"
  156. #define CSI        "\233"
  157.  
  158. void OpenFaxFile(int startFromTextPrint) {
  159.     char tmp[256];
  160.     FILE* seq;
  161.     static int sequence;
  162.     char* filename = tmp;
  163.  
  164.     if (currentPage == 1) {
  165.       /* only do these things once per session -- as signified by
  166.      an open printer */
  167.  
  168.       sequence = 1;
  169.  
  170.       seq = fopen("faxout:seq", "r");
  171.       if (seq) {
  172.     fscanf(seq, "%d", &sequence);
  173.     fclose(seq);
  174.       }
  175.       
  176.       sprintf(lastFaxFile, "fax%d", sequence);
  177.  
  178.       /* update sequence */
  179.       seq = fopen("faxout:seq", "w");
  180.       if (seq) {
  181.     sequence++;
  182.     fprintf(seq, "%d", sequence);
  183.     fclose(seq);
  184.       } else {
  185.     /* danger!  couldn't write to sequence file! */
  186.       }
  187.     }
  188.  
  189.     sprintf(filename, "faxout:%s.%03d", lastFaxFile, currentPage);
  190.     setFileName(filename);
  191.  
  192.     if (startFromTextPrint) {
  193.       if (PD->pd_Preferences.PrintQuality & LETTER) {
  194.     FaxFine = 1;
  195.     setStatus("Opened page for text (Fine)");
  196.       } else {
  197.     FaxFine = 0;
  198.     setStatus("Opened page for text (Standard)");
  199.       }
  200.     } else {
  201.       FaxFine = PED->ped_YDotsInch > Y_DPI/2;
  202.       if (FaxFine)
  203.     setStatus("Opened page for graphics (Fine)");
  204.       else
  205.     setStatus("Opened page for graphics (Standard)");
  206.     }
  207. //    printf("FaxFine = %d\n", FaxFine);
  208.  
  209.     if (filename)
  210.     FaxHandle = faxout_open(filename, 0 /* 0 == output header */);
  211.     if (FaxHandle) {
  212. //      printf("Opened %s\n", filename);
  213.       /* this is generated by a program, so it is *clean* */
  214.       
  215.       faxout_begin_page(FaxHandle, FaxFine, 0);
  216.     }
  217.  
  218.     /* reset, set x offset */
  219.     sprintf(tmp, ESC"c" CSI"%dx", 0);
  220.     WinWrite(tmp);
  221.     return;
  222. }
  223.  
  224. void DeleteLastFaxFile(void) {
  225. //  printf("Deleting last faxfile %s\n", lastFaxFile);
  226.   unlink(lastFaxFile);
  227. }
  228.  
  229. void CloseFaxFile(void) {
  230.   finalizeDoChar();
  231.   if (FaxHandle) {
  232.     setFileName("");
  233.     setStatus("Closed page");
  234. //    printf("Closed %s\n", lastFaxFile);
  235.     faxout_end_page(FaxHandle);
  236.     faxout_close(FaxHandle);
  237.     FaxHandle = NULL;
  238.     currentPage++;
  239.   }
  240. }
  241.  
  242.